package com.talent.service.iot.mqtt.utils;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.mqtt.MqttMessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName：TlLogUtils
 * @Author: hszhou
 * @Date: 2024/12/2 13:16
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
public class TlLog {


    private final static Logger logger = LoggerFactory.getLogger(TlLog.class);
    /**
     * 打印完整字节的
     * @param buf
     */
    public static void logger(String prefix,ByteBuf buf){
        byte[] bytes=new byte[buf.readableBytes()];
        buf.copy().readBytes(bytes);
        StringBuffer sb = new StringBuffer(bytes.length);
        String sTemp;
        for (int i = 0; i < bytes.length; i++) {
            sTemp = Integer.toHexString(0xFF & bytes[i]);
            if (sTemp.length() < 2)
                sTemp = '0' + sTemp;
            sb.append(" ").append(sTemp.toUpperCase());
        }
        logger.debug("{}的16进制的数据为【{}】", prefix, sb);

    }

    public static void main(String[] args) {

        int num= MqttMessageType.PUBLISH.value() << 4;
        String binaryStr = Integer.toBinaryString(num);


        // 将二进制数的第二位和第三位变为01
        int result01 = num & ~(3 << 4) | (1 << 5);
        System.out.println("第二位和第三位变为01后的二进制数: " + Integer.toBinaryString(result01));

        // 将二进制数的第二位和第三位变为10
        int result10 = num & ~(3 << 4) | (2 << 4);
        System.out.println("第二位和第三位变为10后的二进制数: " + Integer.toBinaryString(result10));
    }
}
